﻿Public Class frmRadialGauge
    Private Enum GaugeAnimation As Integer
        Increase = 0
        Decrease = 1
    End Enum
    Private _AnimateGaugeDirection As GaugeAnimation = GaugeAnimation.Increase

    Private Sub frmRadialGauge_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RadialGauge1.Value = 0
        _AnimateGaugeDirection = GaugeAnimation.Increase
        Timer1.Enabled = False
        Timer1.Interval = 10
        btnStartGauge.Text = "Start Gauge"


        'Set up data sources from FillStyle and FillGradientStyle combo boxes
        cboFillGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.RadialGauge.BackgroundType.FillStyles))

        cboDialGradientStyle.DataSource = System.Enum.GetValues(GetType(System.Drawing.Drawing2D.LinearGradientMode))
        cboDialFillStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.RadialGauge.DialBackgroundType.FillStyles))
        cboDialStyle.DataSource = System.Enum.GetValues(GetType(PolyMonControls.RadialGauge.DialBackgroundType.DialFillModes))
        cboLabelPosition.DataSource = System.Enum.GetValues(GetType(PolyMonControls.RadialGauge.MajorLabelsType.MajorLabelPositions))

        'Bind Form objects to Gradient Panel Properties
        cboFillGradientStyle.DataBindings.Add("SelectedItem", RadialGauge1.Background, "FillGradientStyle")
        cboFillStyle.DataBindings.Add("SelectedItem", RadialGauge1.Background, "FillStyle")
        pbBackColor1.DataBindings.Add("BackColor", RadialGauge1.Background, "BackColor1")
        pbBackColor2.DataBindings.Add("BackColor", RadialGauge1.Background, "BackColor2")

        cboDialGradientStyle.DataBindings.Add("SelectedItem", RadialGauge1.DialBackground, "FillGradientStyle")
        cboDialFillStyle.DataBindings.Add("SelectedItem", RadialGauge1.DialBackground, "FillStyle")
        cboDialStyle.DataBindings.Add("SelectedItem", RadialGauge1.DialBackground, "DialStyle")
        pbDialColor1.DataBindings.Add("BackColor", RadialGauge1.DialBackground, "BackColor1")
        pbDialColor2.DataBindings.Add("BackColor", RadialGauge1.DialBackground, "BackColor2")
        nudExtrusionPercentage.DataBindings.Add("Value", RadialGauge1.DialBackground, "ExtrusionPercentage")

        pbNeedleColor.DataBindings.Add("BackColor", RadialGauge1.Needle, "Color")
        pbCenterColor.DataBindings.Add("BackColor", RadialGauge1.Needle, "CenterColor")
        nudCenterRadius.DataBindings.Add("Value", RadialGauge1.Needle, "CenterRadius")
        nudNeedleThickness.DataBindings.Add("Value", RadialGauge1.Needle, "Thickness")
        nudPercentageLength.DataBindings.Add("Value", RadialGauge1.Needle, "PercentageLength")

        pbAxisColor.DataBindings.Add("BackColor", RadialGauge1, "AxisColor")
        nudAxisThickness.DataBindings.Add("Value", RadialGauge1, "AxisThickness")

        nudMajorTickCount.DataBindings.Add("Value", RadialGauge1.MajorTicks, "NumTicks")
        nudMajorTickSize.DataBindings.Add("Value", RadialGauge1.MajorTicks, "TickSize")
        nudMajorTickThickness.DataBindings.Add("Value", RadialGauge1.MajorTicks, "TickThickness")
        pbMajorTickColor.DataBindings.Add("BackColor", RadialGauge1.MajorTicks, "TickColor")

        pbLabelColor.DataBindings.Add("BackColor", RadialGauge1, "ForeColor")
        chkDisplayMajorLabels.DataBindings.Add("Checked", RadialGauge1.MajorLabels, "Display")
        cboLabelPosition.DataBindings.Add("SelectedItem", RadialGauge1.MajorLabels, "Position")
        nudLabelPadding.DataBindings.Add("Value", RadialGauge1.MajorLabels, "Padding")

        nudMinorTickCount.DataBindings.Add("Value", RadialGauge1.MinorTicks, "NumTicks")
        nudMinorTickSize.DataBindings.Add("Value", RadialGauge1.MinorTicks, "TickSize")
        nudMinorTickThickness.DataBindings.Add("Value", RadialGauge1.MinorTicks, "TickThickness")
        pbMinorTickColor.DataBindings.Add("BackColor", RadialGauge1.MinorTicks, "TickColor")


        nudStartAngle.DataBindings.Add("Value", RadialGauge1, "StartAngle")
        nudSweep.DataBindings.Add("Value", RadialGauge1, "Sweep")
        nudRangeStart.DataBindings.Add("Value", RadialGauge1, "RangeStart")
        nudRangeEnd.DataBindings.Add("Value", RadialGauge1, "RangeEnd")

        pbArc1Color.DataBindings.Add("BackColor", RadialGauge1.HeatArc1, "Color")
        nudArc1Width.DataBindings.Add("Value", RadialGauge1.HeatArc1, "Width")
        nudArc1RangeStart.DataBindings.Add("Value", RadialGauge1.HeatArc1, "RangeStart")
        nudArc1RangeEnd.DataBindings.Add("Value", RadialGauge1.HeatArc1, "RangeEnd")

        pbArc2Color.DataBindings.Add("BackColor", RadialGauge1.HeatArc2, "Color")
        nudArc2Width.DataBindings.Add("Value", RadialGauge1.HeatArc2, "Width")
        nudArc2RangeStart.DataBindings.Add("Value", RadialGauge1.HeatArc2, "RangeStart")
        nudArc2RangeEnd.DataBindings.Add("Value", RadialGauge1.HeatArc2, "RangeEnd")

        pbArc3Color.DataBindings.Add("BackColor", RadialGauge1.HeatArc3, "Color")
        nudArc3Width.DataBindings.Add("Value", RadialGauge1.HeatArc3, "Width")
        nudArc3RangeStart.DataBindings.Add("Value", RadialGauge1.HeatArc3, "RangeStart")
        nudArc3RangeEnd.DataBindings.Add("Value", RadialGauge1.HeatArc3, "RangeEnd")

        'Add handlers for color selections
        AddHandler pbBackColor1.Click, AddressOf ChooseColor
        AddHandler pbBackColor2.Click, AddressOf ChooseColor

        AddHandler pbDialColor1.Click, AddressOf ChooseColor
        AddHandler pbDialColor2.Click, AddressOf ChooseColor
        AddHandler pbCenterColor.Click, AddressOf ChooseColor
        AddHandler pbNeedleColor.Click, AddressOf ChooseColor
        AddHandler pbAxisColor.Click, AddressOf ChooseColor
        AddHandler pbLabelColor.Click, AddressOf ChooseColor
        AddHandler pbMajorTickColor.Click, AddressOf ChooseColor
        AddHandler pbMinorTickColor.Click, AddressOf ChooseColor
        AddHandler pbArc1Color.Click, AddressOf ChooseColor
        AddHandler pbArc2Color.Click, AddressOf ChooseColor
        AddHandler pbArc3Color.Click, AddressOf ChooseColor

        'Add Handler for Form object property changes
        AddHandler cboFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboFillGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbBackColor2.BackColorChanged, AddressOf ControlValueChanged

        AddHandler cboDialFillStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboDialGradientStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler cboDialStyle.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler nudExtrusionPercentage.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbDialColor1.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbDialColor2.BackColorChanged, AddressOf ControlValueChanged

        AddHandler pbCenterColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler pbNeedleColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudCenterRadius.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudNeedleThickness.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudPercentageLength.ValueChanged, AddressOf ControlValueChanged

        AddHandler pbAxisColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudAxisThickness.ValueChanged, AddressOf ControlValueChanged

        AddHandler nudMajorTickCount.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudMajorTickSize.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudMajorTickThickness.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbMajorTickColor.BackColorChanged, AddressOf ControlValueChanged

        AddHandler pbLabelColor.BackColorChanged, AddressOf ControlValueChanged
        AddHandler chkDisplayMajorLabels.CheckedChanged, AddressOf ControlValueChanged
        AddHandler cboLabelPosition.SelectedIndexChanged, AddressOf ControlValueChanged
        AddHandler nudLabelPadding.ValueChanged, AddressOf ControlValueChanged

        AddHandler nudMinorTickCount.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudMinorTickSize.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudMinorTickThickness.ValueChanged, AddressOf ControlValueChanged
        AddHandler pbMinorTickColor.BackColorChanged, AddressOf ControlValueChanged

        AddHandler nudStartAngle.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudSweep.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudRangeStart.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudRangeEnd.ValueChanged, AddressOf ControlValueChanged

        AddHandler pbArc1Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudArc1Width.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudArc1RangeStart.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudArc1RangeEnd.ValueChanged, AddressOf ControlValueChanged

        AddHandler pbArc2Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudArc2Width.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudArc2RangeStart.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudArc2RangeEnd.ValueChanged, AddressOf ControlValueChanged

        AddHandler pbArc3Color.BackColorChanged, AddressOf ControlValueChanged
        AddHandler nudArc3Width.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudArc3RangeStart.ValueChanged, AddressOf ControlValueChanged
        AddHandler nudArc3RangeEnd.ValueChanged, AddressOf ControlValueChanged

    End Sub



    Private Sub btnStartGauge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartGauge.Click
        If Timer1.Enabled Then
            Timer1.Enabled = False
            btnStartGauge.Text = "Start Gauge"
        Else
            Timer1.Enabled = True
            btnStartGauge.Text = "Stop Gauge"
        End If
        
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        If RadialGauge1.Value >= RadialGauge1.RangeEnd AndAlso _AnimateGaugeDirection = GaugeAnimation.Increase Then
            _AnimateGaugeDirection = GaugeAnimation.Decrease
        ElseIf RadialGauge1.Value <= RadialGauge1.RangeStart AndAlso _AnimateGaugeDirection = GaugeAnimation.Decrease Then
            _AnimateGaugeDirection = GaugeAnimation.Increase
        End If

        Select Case _AnimateGaugeDirection
            Case GaugeAnimation.Increase
                If RadialGauge1.Value >= RadialGauge1.RangeEnd Then
                    _AnimateGaugeDirection = GaugeAnimation.Decrease
                Else
                    RadialGauge1.Value += 1
                End If
            Case GaugeAnimation.Decrease
                If RadialGauge1.Value <= RadialGauge1.RangeStart Then
                    _AnimateGaugeDirection = GaugeAnimation.Increase
                Else
                    RadialGauge1.Value -= 1
                End If
        End Select
    End Sub

    Private Sub ChooseColor(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = CType(sender, PictureBox)
        With Me.ColorDialog1
            .Color = pb.BackColor
            Dim res As DialogResult = .ShowDialog()

            If res = Windows.Forms.DialogResult.OK Then
                pb.BackColor = .Color
            End If
        End With
    End Sub

    Private Sub ControlValueChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim Ctl As Control = CType(sender, Control)
        If Ctl.DataBindings.Count > 0 Then
            Ctl.DataBindings(0).WriteValue()
        End If
    End Sub

    Private Sub cboDialStyle_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboDialStyle.SelectedIndexChanged
        If CType(cboDialStyle.SelectedItem, PolyMonControls.RadialGauge.DialBackgroundType.DialFillModes) = PolyMonControls.RadialGauge.DialBackgroundType.DialFillModes.Extruded Then
            nudExtrusionPercentage.Enabled = True
        Else
            nudExtrusionPercentage.Enabled = False
        End If
    End Sub
End Class